import 'package:ball_lion/entity/king/king_entity.dart';
import 'package:ball_lion/utils/utils.dart';

class PlayEntity {
  String? playType;
  int? matchId;
  String? handicap;
  List<String>? items;

  PlayEntity({this.playType, this.matchId, this.handicap, this.items});
}

class PlayResult {
  String? playType;
  String? handicap;
  List<String>? choices;

  PlayResult({this.handicap, this.choices, this.playType});
}

Map<String, String> playType = {
  "胜平负": "1001",
  "让球胜平负": "1002",
  "比分": "1003",
  "总进球": "1004",
  "半场胜平负": "1005",
  "让球": "1006",
  "亚盘": "1006",
  "大小球": "1007",
  "双平": "1008",
  "胜负": "2001",
  "让球胜负": "2002",
  "大小分": "2003"
};

Map<String, String> matchResult = {"胜": "3", "平": "1", "负": "0"};
Map<String, String> matchHomeResult = {"主胜": "3", "平局": "1", "客胜": "0"};
Map<int, String> codeToResult = {3: "主胜", 1: "平局", 0: "客胜"};
Map<String, String> matchOorU = {"大": "1", "小": "2"};
Map<String, String> oddsResult = {"s": "主胜", "p": "平局", "f": "客胜"};

String encodePlayType(List<PlayEntity> list) {
  List<String> playList = [];
  for (var entity in list) {
    switch (playType[entity.playType]) {
      case "1001":
      case "1002":
        if (entity.items.hasValue) {
          playList.add("${playType[entity.playType]}-${entity.matchId}("
              "${List.generate(entity.items?.length ?? 0, (index) => "${entity.handicap != "0" && entity.handicap.hasValue ? "${entity.handicap}:" : ""}"
                  "${matchHomeResult[entity.items?[index]]}_1").join(',')})");
        }
        break;
      case "1004":
        if (entity.items.hasValue) {
          playList.add("1004-${entity.matchId}("
              "${List.generate(entity.items?.length ?? 0, (index) => "${entity.items?[index]}_1").join(',')})");
        }
    }
  }
  return "${playList.length}#${playList.join("|")}";
}

RegExp format =
    RegExp(r"\d+-\d+\((-*\d:)*\d+_\d+(\.\d+)*(,(-*\d:)*\d+_\d+(\.\d+)*)*\)");

List<PlayResult> decodePlayType(String item) {
  List<PlayResult> result = [];
  item = item.replaceFirst(RegExp(r"\d+#"), "");
  List<String> list = item.split('|');
  for (var element in list) {
    List<String> type = [];
    type.add(element.substring(0, 4));
    List<String> list = element
        .replaceFirst(RegExp(r"\d+-\d+\("), "")
        .replaceFirst(")", "")
        .replaceAll(RegExp(r"_\d+(\.\d+)*"), "")
        .split(",");
    for (var element in type) {
      switch (element) {
        case "1001":
        case "1002":
          String handicap = element == "1001" ? "0" : list[0].split(":")[0];
          for (var element in list) {
            element.replaceFirst(RegExp(r"-*\d:"), "");
          }
          result.add(PlayResult(
              playType: element,
              handicap: handicap,
              choices: List.generate(list.length, (index) {
                switch (list[index].replaceFirst(RegExp(r"-*\d:"), "")) {
                  case "3":
                    return "主胜";
                  case "1":
                    return "平局";
                  case "0":
                    return "客胜";
                  default:
                    return "未知";
                }
              })));
          break;
        case "1004":
          result.add(PlayResult(
              playType: element,
              choices: List.generate(
                  list.length,
                  (index) =>
                      "${list[index].toInt()!}球${list[index].toInt()! == 7 ? "及以上" : ""}")));
          break;
      }
    }
  }
  return result;
}

PlayResult? decodePlayResult(PlayTypeEntity entity) {
  switch (entity.playType) {
    case 1001:
      PlayTypeItem? e = entity.spf?.list?.firstWhere(
          (element) => element.r == 1,
          orElse: () => PlayTypeItem());
      PlayTypeItem? item;
      if (e?.r == 1) item = e!;
      return PlayResult(
          handicap: "0",
          playType: entity.playType.toString(),
          choices: [oddsResult[item?.i ?? ""] ?? ""]);
    case 1002:
      List<PlayTypeItem> items = [];
      entity.rqSpf?.forEach((element) {
        PlayTypeItem? e = element.list?.firstWhere((element) => element.r == 1,
            orElse: () => PlayTypeItem());
        if (e?.r == 1) items.add(e!);
      });
      return PlayResult(
          handicap: entity.rqSpf?.first.line ?? "0",
          playType: entity.playType.toString(),
          choices: List.generate(
              items.length, (index) => oddsResult[items[index].i] ?? ""));
    case 1004:
      PlayTypeItem? e = entity.jqs?.list?.firstWhere(
          (element) => element.r == 1,
          orElse: () => PlayTypeItem());
      PlayTypeItem? item;
      if (e?.r == 1) item = e!;
      return PlayResult(
          handicap: "0",
          playType: entity.playType.toString(),
          choices: ["${item?.p}球${item?.p == "7" ? "及以上" : ""}"]);
    default:
      return null;
  }
}
